package com.example.cd5160866.urlapp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

/**
 * Created by cd5160866 on 15/10/21.
 */
public class AssginScrollView extends HorizontalScrollView {
    private OnScrollChangeListenter listenter;

    public AssginScrollView(Context context) {
        super(context);
    }

    public AssginScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AssginScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnScrollChangeListenter(OnScrollChangeListenter listenter) {
        this.listenter = listenter;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        currentY = l;
        if (listenter != null) {
            try {
                int parcent = (l * 100 / (computeHorizontalScrollRange() - getWidth()));
                currentParcent = parcent;
                listenter.onScrollChanged(parcent);
            } catch (ArithmeticException e) {
                e.printStackTrace();
            }
        }
    }

    public interface OnScrollChangeListenter {
        void onScrollChanged(int parcent);
    }

    int currentY = 0;

    int currentParcent = 0;

    public int getCurrentY() {
        return currentY;
    }

    public int getCurrentParcent() {
        return currentParcent;
    }

    private boolean mIntercept = true;

    public void setIntercepTouchEvent(boolean intercept) {
        this.mIntercept = intercept;
    }

//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        return super.dispatchTouchEvent(ev);
//    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (mIntercept)
            return super.onInterceptTouchEvent(ev);
        return mIntercept;
    }
}
